//
// Created by DELL on 2022/4/26.
//
#pragma once
#include "../Utils.h"
#include "../VulkanDefine.h"
#include "../MathHelp.h"

BEGIN_NS_VULKAN

class Texture
{
public:

    ~Texture();
    Texture(const Texture&) = delete;
    Texture& operator=(const Texture&) = delete;

    static TextureRef create(const DeviceRef& device, const std::string& path, bool dynamicUpdate = false);
    static TextureRef create(const DeviceRef& device, int width, int height, VkFormat format);

    int getWidth() const { return this->width; }
    int getHeight() const { return this->height; }
    math::Vec2 getSize() const { return {static_cast<float>(width), static_cast<float>(height)}; }

    VkImage getImage() const { return this->image; }
    VkDeviceMemory getImageMemory() const { return this->imageMemory; }
    VkImageView getImageView() const { return this->imageView; }
    VkFormat getFormat() const { return this->imageFormat; }

    void update(uint8_t* pixelData, int32_t dataSize);

private:
    Texture(DeviceRef device, bool dynamicUpdate);
    bool init(const std::string& path);
    bool init(int width, int height, VkFormat format);
    void transitionImageLayout(VkImageLayout oldLayout, VkImageLayout newLayout);

    int width{};
    int height{};
    bool dynamicUpdate{false};

    DeviceRef device;
    VkImage image{};
    VkDeviceMemory imageMemory{};
    VkImageView imageView{};
    VkFormat imageFormat{};
};

END_NS_VULKAN